// pages/sell/sell.js

import { request } from "../../request/index.js";

const goodsStatus = {
  out: 0,
  in: 1,
  done: 2
}

Page({

  /**
   * 页面的初始数据
   */
  data: {

    all_mark: "all_mark1",
    all_isShow: true ,
    out_mark: "out_mark1",
    out_isShow: true ,
    in_mark: "in_mark1",
    in_isShow: true ,
    orders: [],
    // 待发货的数组
    ordersOut: [],
    // 待收货的数组
    ordersIn: [],
  },

  // top 点击改变全部样式
  all_mark: function () {
    this.setData({
      all_mark: "all_mark2",
      all_isShow: true ,
      out_mark: "out_mark1",
      out_isShow: false ,
      in_mark: "in_mark1",
      in_isShow: false ,
    })
  },
  // top 点击改变待发货样式
  out_mark: function () {
    this.setData({
      out_mark: "out_mark2",
      out_isShow: true ,
      all_mark: "all_mark1",
      all_isShow: false ,
      in_mark: "in_mark1",
      in_isShow: false ,
    })
  },
  // top 点击改变待收货样式
  in_mark: function () {
    this.setData({
      in_mark: "in_mark2",
      in_isShow: true ,
      all_mark: "all_mark1",
      all_isShow: false ,
      out_mark: "out_mark1",
      out_isShow: false ,
    })
  },

  // 点击待发货区的选择框
  changeOut: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var select = e.currentTarget.dataset.select

    if (select == "circle") {
      var stype = "success"
    } else {
      var stype = "circle"
    }
    var newList = that.data.ordersOut
    newList[index].select = stype
    that.setData({
      ordersOut: newList
    })
    console.log(this.data.ordersOut)

    var newList = that.data.ordersOut
    for (var i = 0; i < newList.length; i++) {
      if( newList[i].select === "circle" )
      {
        that.setData({
          allSelect: 'circle'
        })
        break
      }
      else{
        that.setData({
          allSelect: 'success'
        })
      }
    }
  },

// 点击确认发货
confirm(){
  var that = this
  let readyConfirm = []
  console.log(this.data.ordersOut)
  this.data.ordersOut.forEach(r => {
    if (r.select == 'success') {
      readyConfirm.push(r.outTradeNo)
    }
  })
  if( readyConfirm.length!=0){
    wx.showModal({
      content: '您确定要确认发货么',
      cancelText: '取消',
      confirmText: '确定',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          request({
            url: '/book/seller/confirmShipment?'+"outTradeNo="+readyConfirm, 
            method: "GET"
          })
            .then(result => {
              console.log(result)
              that.onShow()
            })
            
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    console.log(readyConfirm)
  }
  else{
    wx.showToast({
      title: '还没选择宝贝哦',
      icon: 'none',
      duration: 1000
    })
  }
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    request({ url:'/userPage/getSell'})
    .then(result=>{
      console.log(result)
      let listOut = []
        let listIn = []
        let list = result.data.data.list
        list.forEach((r) => {
          r.select = 'circle';
          // 待发货
          if (r.status === goodsStatus.out) {
            listOut.push(r)
            r.status = '待发货'
          }
          // 已发货 待收货
          else if (r.status === goodsStatus.in) {
            listIn.push(r)
            r.status = '待收货'
          }
          // 已完成
          else if (r.status === goodsStatus.done) {
            r.status = '已完成'
          }
        })
        console.log(list)
        that.setData({
          ordersOut: listOut,
          ordersIn: listIn,
          orders: list,
          // count: result.data.data.list.length
        })
        console.log(this.data.orders)
        console.log(this.data.ordersOut)
        console.log(this.data.ordersIn)
      })
    that.all_mark()
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})